'''   Copyright [八月不落东方叶] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''


import sqlite3
from time import time,sleep
from exec import crezip
from sql import sort2
from os import listdir,remove

ti = 60*60*24

def such():#搜索数据库中已启动的文件
    db = sqlite3.connect('data\data.db')
    cur = db.cursor()
    cur.execute("SELECT * FROM task where status == '┗|｀O′|┛ 嗷~~'")
    db.commit()
    a = cur.fetchall()
    cur.close()
    db.close()
    sort2(a)
    return a

def run(a):#执行自动备份
    for i in range(len(a)):
        try:
            start = time()
            print(a[i][2],'-->',a[i][3]+'\\'+a[i][1]+'.zip')
            crezip(a[i][2],a[i][3]+'\\'+a[i][1]+'.zip')
            end = time()
            print('---------------------------------------------------------------------------------')
            print('已备份'+a[i][1]+'.zip,共耗时：%s Seconds'%(end-start))
            print('---------------------------------------------------------------------------------')
        except:
            print('---------------------------------------------------------------------------------')
            print('本次{a[i][1]}.zip备份失败，已自动跳过本次备份')
            print('---------------------------------------------------------------------------------')            



def dele(a):#删除多余备份文件
    
    for i in range(len(a)):
        d=[]
        e=[]
        c=0
        b = listdir(a[i][3])
        for j in b:
            if a[i][1] in j:
                c+=1
                d.append(j)
                e.append(j)
        for r in range(c):
            if c > a[i][4]:
                for g in range(len(d)):
                    d[g]=d[g][-23:-5]
                d.sort()
                for x in range(len(b)):
                    if d[0] in e[x]:
                        try:
                            remove(a[i][3]+'\\'+e[x])
                            c-=1
                            print('---------------------------------------------------------------------------------')
                            print('已删除：',e[x])
                            print('---------------------------------------------------------------------------------')
                            del d[0]
                            break
                        except:
                            c-=1
                            del d[0]
                            print('---------------------------------------------------------------------------------')
                            print(e[x],'删除失败，已删除本次备份。。')
                            print('---------------------------------------------------------------------------------')
                            break
            else:
                break
        


if __name__ == '__main__':
    i =0
    while True:
        i+=1
        print('---------------------------------------------------------------------------------')
        print('已执行%d次'%i)
        print('---------------------------------------------------------------------------------')
        try:
            a= such()
            if len(a) != 0:
                run(a)
                dele(a)
                sleep(ti)
            else:
                sleep(ti)
        except:
            sleep(ti)
